<template>
  <div>
    <el-form :model="singleData" ref="singleData" label-width="100px" class="demo-singleData">
      <el-form-item label="文章标题" prop="name">
        <el-input type="textarea" v-model="singleData.title"></el-input>
      </el-form-item>
      <el-form-item label="文章分类" prop="region">
        <el-select v-model="singleData.category" placeholder="请选择活动区域">
          <el-option label="区域一" value="shanghai"></el-option>
          <el-option label="区域二" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文章概要" prop="name">
        <el-input type="textarea" v-model="singleData.summary"></el-input>
      </el-form-item>
      <el-form-item label="文章内容" prop="name">
        <VueUEditor></VueUEditor>
      </el-form-item>
      <el-form-item label="热门" prop="resource">
        <el-radio-group v-model="singleData.is_hot">
          <el-radio label="是"></el-radio>
          <el-radio label="否 "></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="浏览次数" prop="desc">
        <el-input disabled type="text" v-model="singleData.view"></el-input>
      </el-form-item>
      <el-form-item label="评论次数" prop="desc">
        <el-input disabled type="text" v-model="singleData.comment_count"></el-input>
      </el-form-item>
      <el-form-item label="收藏次数" prop="desc">
        <el-input disabled type="text" v-model="singleData.like_count"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="desc">
        <el-input disabled type="text" v-model="singleData.create_time"></el-input>
      </el-form-item>
      <el-form-item>
        <!-- <el-button type="danger" @click="submitForm('singleData')">保存</el-button> -->
        <el-button type="danger" @click="submit">保存</el-button>
        <!-- <el-button @click="resetForm('singleData')">重置</el-button> -->
        <el-button @click="$router.back()">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import VueUEditor from 'vue-ueditor'
export default {
  components: {
    VueUEditor
  },
  data () {
    return {
      singleData: {
        title: '',
        summary: '',
        content: '',
        is_hot: 0,
        view: 0,
        like_count: 0,
        comment_count: 0,
        create_time: 0,
        admin_tid: 1 // 暂时写死
      }
    }
  },
  computed: {
    isEdit () {
      // if (this.$route.params.article_tid) {
      //   return true
      // } else {
      //   return false
      // }
      return false
    }
  },
  mounted () {
    if (this.isEdit) {
      this._getData()
    }
  },
  methods: {
    submit () {
      if (this.isEdit) {
        this._updateSingleData()
      } else {
        this._createSingleData()
      }
    },
    _updateSingleData () {
      // 还有tid
      delete this.singleData.view
      delete this.singleData.like_count
      delete this.singleData.comment_count
      updateArticle(this.singleData).then(res => {
      })
    },
    _createSingleData () {
      createArticle(this.singleData).then(res => {
        this.$router.back()
      })
    },
    _getData () {
      getArticle({tid: this.$route.params.article_tid}).then(res => {
        const singleData = res.data
        this.singleData = {
          title: singleData.title,
          summary: singleData.summary,
          content: singleData.content,
          is_hot: singleData.is_hot,
          view: singleData.view,
          like_count: singleData.like_count,
          comment_count: singleData.comment_count,
          create_time: singleData.create_time
        }
      })
    },
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!')
        } else {
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>

</style>
